package org.libspark.fmvc.util.reflection.impl
{
	import org.libspark.fmvc.util.reflection.IArgument;
	import org.libspark.fmvc.util.reflection.IMetaData;
	import org.libspark.fmvc.util.reflection.IMetaDataAware;

	public class MetaData implements IMetaData
	{
		private var _metaDataAware:IMetaDataAware

		private var _name:String;

		public function MetaData(xml:XML, metaDataAware:IMetaDataAware)
		{
			_metaDataAware = metaDataAware;
			_name = xml.@name;
			_args = [];
			_argMap = {};

			var argList:XMLList = xml.arg;
			for (var i:int = 0, len:int = argList.length(); i < len; i++)
			{
				var arg:IArgument = new Argument(argList[i], this);
				_args.push(arg);

				var n:String = arg.name;
				if (n in _argMap)
				{
					_argMap[n] = [];
				}
				_argMap[n].push(arg);
			}
		}

		public function get name():String
		{
			return _name;
		}

		public function get metaDataAware():IMetaDataAware
		{
			return _metaDataAware;
		}

		public function get argument():IArgument
		{
			return _args[0];
		}

		public function getArguments():Array
		{
			return _args.concat();
		}

		public function getArgumentsByName(name:String):Array
		{
			var args:Array = _argMap[name];
			if (args === null || args.length === 0)
			{
				return [];
			}

			return args.concat();
		}

		public function getArgumentByName(name:String):IArgument
		{
			var args:Array = _argMap[name];
			if (args === null)
			{
				return null;
			}

			return args[0];
		}


		public function getArgument(index:int):IArgument
		{
			return _args[index];
		}

		public function get argumentLength():uint
		{
			return _args.length;
		}
	}
}